package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.repository.UserMapper;
import cn.livingCloud.utils.DateUtils;
import cn.livingCloud.utils.UuidGenerator;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 案件档案
 */
public class CaseRecord extends BaseEntity {
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient UserMapper userMapper = BeanProvider.getBean(UserMapper.class);

    private String uuid = UuidGenerator.generate();

    private Date createDateTime = DateUtils.now();

    private String caseUuid;

    //操作员工(Employee)id
    private String operator;

    private Boolean archive = false;

    private CaseRecordType type;

    private String oldOwnerId;

    private String newOwnerId;

    private String content;

    //操作用户(User)id
    private String operatorUserUuid;

    public CaseRecord() {
    }

    public String getUuid() {
        return uuid;
    }

    public CaseRecord setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    @Override
    public Date getCreateDateTime() {
        return createDateTime;
    }

    @Override
    public CaseRecord setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
        return this;
    }

    public String getCaseUuid() {
        return caseUuid;
    }

    public CaseRecord setCaseUuid(String caseUuid) {
        this.caseUuid = caseUuid;
        return this;
    }

    public String getOperator() {
        return operator;
    }

    public CaseRecord setOperator(String operator) {
        this.operator = operator;
        return this;
    }

    @Override
    public Boolean getArchive() {
        return archive;
    }

    @Override
    public CaseRecord setArchive(Boolean archive) {
        this.archive = archive;
        return this;
    }

    public CaseRecordType getType() {
        return type;
    }

    public CaseRecord setType(CaseRecordType type) {
        this.type = type;
        return this;
    }

    public String getOldOwnerId() {
        return oldOwnerId;
    }

    public CaseRecord setOldOwnerId(String oldOwnerId) {
        this.oldOwnerId = oldOwnerId;
        return this;
    }

    public String getNewOwnerId() {
        return newOwnerId;
    }

    public CaseRecord setNewOwnerId(String newOwnerId) {
        this.newOwnerId = newOwnerId;
        return this;
    }

    public String getContent() {
        return content;
    }

    public CaseRecord setContent(String content) {
        this.content = content;
        return this;
    }

    public String getOperatorUserUuid() {
        return operatorUserUuid;
    }

    public CaseRecord setOperatorUserUuid(String operatorUserUuid) {
        this.operatorUserUuid = operatorUserUuid;
        return this;
    }

    public Employee getOperatorEmployee() {
        return employeeMapper.selectByPrimaryKey(this.operator);
    }

    @Deprecated
    public Employee getOldOwner() {
        return employeeMapper.selectByPrimaryKey(this.oldOwnerId);
    }

    @Deprecated
    public Employee getNewOwner() {
        return employeeMapper.selectByPrimaryKey(this.newOwnerId);
    }

    public User getOperatorUser() {
        return userMapper.findUserByUuid(this.operatorUserUuid);
    }

    public List<Employee> getNewOwnerList() {
        if (StringUtils.isEmpty(newOwnerId)) {
            return new ArrayList<>();
        }
        List<String> owners = Arrays.asList(this.newOwnerId.split(","));
        if (owners.size() == 0) {
            return new ArrayList<>();
        }

        return employeeMapper.findEmployees(owners);
    }

    public List<Employee> getOldOwnerList() {
        if (StringUtils.isEmpty(oldOwnerId)) {
            return new ArrayList<>();
        }
        List<String> owners = Arrays.asList(this.oldOwnerId.split(","));
        if (owners.size() == 0) {
            return new ArrayList<>();
        }

        return employeeMapper.findEmployees(owners);
    }
}